package com.doremi.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;

import com.doremi.dao.DaoLayer;
import com.doremi.presentation.activities.MainActivity;
import com.doremi.util.Album;
import com.doremi.util.Artist;
import com.doremi.util.Track;

/**
 * This class implement the DaoLayer. The internal implementation depends on
 * Android interface.
 * 
 * @author alejo
 * 
 */

public class DaoLayerImpl implements DaoLayer {

	private ContentResolver source;
	
	public DaoLayerImpl()
	{
		setDataSource(MainActivity.getContentRes());
	}
	
	public List<Artist> getAllArtits(String artistMatchingKey) {
		return this.getArtist(artistMatchingKey);
	}

	public List<Album> getAllAlbums(String artistName) {
		return this.getAlbums(artistName);
	}

	public List<Track> getAllTracks(String artistName,String albumName) {
		return this.getTracks(artistName,albumName);
	}

	public void setDataSource(Object source) {
		this.source = (ContentResolver)source;
	}

	public ContentResolver getSource() {
		return source;
	}
	
	/* Private methods */

	private List<Album> getAlbums(String artistName) {

		String[] proj = { MediaStore.Audio.Albums.ALBUM,
				MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST };
		
		String selectBody = artistName != null && !artistName.equals("") ? " artist like '%"+artistName.replace("'", "''")+"%' " : null;
		Cursor cur = getSource().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
				proj,selectBody , null, MediaStore.Audio.Albums.ALBUM);
		List<Album> albums = new ArrayList<Album>();

		if (cur.moveToFirst()) {
			do {
				int albumIdIndex = cur
						.getColumnIndex(MediaStore.Audio.Albums._ID);
				int albumIndex = cur
						.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
				int artistIndex = cur
						.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
				int albumId = cur.getInt(albumIdIndex);
				String name = cur.getString(albumIndex);
				String artist = cur.getString(artistIndex);

				Album album = new Album(albumId, name, artist);
				if (!albums.contains(album)) {
					albums.add(album);
				}
			} while (cur.moveToNext());
		}
		cur.close();
		return albums;
	}
	
	private List<Track> getTracks(String artistName,String albumName) {

		String[] proj = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };
		
		String artistNameSelectBody = artistName != null && !artistName.equals("") ? " artist LIKE '%"+artistName.replace("'", "''")+"%' " : null;
		String albumNameSelectBody = albumName != null && !albumName.equals("") ? " album LIKE '%"+albumName.replace("'", "''")+"%' " : null;
		Cursor cur = getSource().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				proj, artistNameSelectBody+"AND"+albumNameSelectBody, null, MediaStore.Audio.Media._ID);
		List<Track> tracks = new ArrayList<Track>();

		if (cur.moveToFirst()) {
			do {
				int trackIdIndex = cur.getColumnIndex(MediaStore.Audio.Media._ID);
				int trackNametIndex = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
				int trackPathIndex = cur.getColumnIndex(MediaStore.Audio.Media.DATA);
				
				int trackId = cur.getInt(trackIdIndex);
				String trackName = cur.getString(trackNametIndex);
				String trackPath = cur.getString(trackPathIndex);
				
				Track track = new Track(trackName,trackPath,trackId);
				if (!tracks.contains(track)) {
					track.setName(trackName);
					track.setPath(trackPath);
					track.setTrackId(trackId);
					track.setArtist(artistName);
					tracks.add(track);
				}
			} while (cur.moveToNext());
		}
		cur.close();
		return tracks;
	}
	
	private List<Artist> getArtist(String artistNameKey) {

		String[] proj = { MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST};
		
		String selectBody = artistNameKey != null && !artistNameKey.equals("") ? " artist like '%"+artistNameKey.replace("'", "''")+"%' " : null;
		Cursor cur = getSource().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
				proj,selectBody , null, MediaStore.Audio.Artists.ARTIST);
		List<Artist> artists = new ArrayList<Artist>();

		if (cur.moveToFirst()) {
			do {
				int artistIdIndex = cur
						.getColumnIndex(MediaStore.Audio.Artists._ID);
				int artistIndex = cur
						.getColumnIndex(MediaStore.Audio.Artists.ARTIST);
				int artistId = cur.getInt(artistIdIndex);
				String artistName = cur.getString(artistIndex);
				
				Artist artist = new Artist(artistName,artistId);
				if (!artists.contains(artist)) {
					artist.setArtistId(artistId);
					artist.setName(artistName);
					artists.add(artist);
				}
			} while (cur.moveToNext());
		}
		cur.close();
		return artists;
	}

}